package cn.nawang.ebeim.ui.controller;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.client.util.PropertiesUtil;
import cn.nawang.ebeim.ui.bean.ConfigBean;

@RestController
public class SystemSetController {
  private static final Logger LOG = LoggerFactory.getLogger(SystemSetController.class);
  /**
   * 
   * @author: huanghj
   * @Description: 设置系统参数
   * @param @param config
   * @param @return
   * @param @throws UnsupportedEncodingException
   * @return String
   * @date 2015年11月11日下午3:11:46
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/systemSet/sysInfo", method = RequestMethod.GET)
  public String setSysInfo(ConfigBean config) throws UnsupportedEncodingException {
    LOG.debug("/systemSet/sysInfo {}", config);
    String apiHost = PropertiesUtil.read("host");
    String port = PropertiesUtil.read("port");
    String lockTime = config.getLockTime();
    JSONObject json = new JSONObject();
    json.put("code", "500");
    Long writeLimit=0l;
    Long readLimit=0l;
    Long lockTimeValue=0l;
    if(!StringUtils.isEmpty(config.getWriteLimit()))
    {
       writeLimit = Long.valueOf(config.getWriteLimit()) * 1000;
    }
    if(!StringUtils.isEmpty(config.getReadLimit()))
    {
       readLimit = Long.valueOf(config.getReadLimit()) * 1000;
    }
    if(!StringUtils.isEmpty(config.getLockTime()))
    {
      lockTimeValue=Long.valueOf(lockTime);
    }
    try {
      if (lockTime.equals("-1")) {
        PropertiesUtil.writeProperties("lock_time", "-1");
        json.put("lockTime", lockTime);
      } else {
        Long lockTimeVal = lockTimeValue * 60 * 1000;
        PropertiesUtil.writeProperties("lock_time", String.valueOf(lockTimeVal));
        json.put("lockTime", lockTimeVal/1000/60);
      }
      PropertiesUtil.writeProperties("write_limit", String.valueOf(writeLimit));
      PropertiesUtil.writeProperties("read_limit", String.valueOf(readLimit));
      json.put("writeLimit", writeLimit / 1000);
      json.put("readLimit", readLimit / 1000);
      json.put("apiHost", apiHost);
      json.put("port", port);
      json.put("code", "200");
    } catch (Exception e) {
      e.printStackTrace();
    }
    return json.toString();
  }


  @RequestMapping(value = "/systemSet/getSysInfo", method = RequestMethod.GET)
  public String getSysInfo() throws UnsupportedEncodingException {
    LOG.debug("/systemSet/getSysInfo {}");
    JSONObject json = new JSONObject();
    String apiHost = PropertiesUtil.read("host");
    String port = PropertiesUtil.read("port");
    String lockTime = PropertiesUtil.read("lock_time");
    if (lockTime==null)
    {
      try {
        PropertiesUtil.writeProperties("lock_time", "-1");
      } catch (Exception e) {
        e.printStackTrace();
      }
      json.put("lockTime", "-1");
    }
    else if (lockTime.equals("-1")) {
      json.put("lockTime", lockTime);
    } else {
      json.put("lockTime", Long.valueOf(lockTime) / 1000 / 60);
    }
    Long writeLimit = Long.valueOf(PropertiesUtil.read("write_limit")) / 1000;
    Long readLimit = Long.valueOf(PropertiesUtil.read("read_limit")) / 1000;
    json.put("writeLimit", writeLimit);
    json.put("readLimit", readLimit);
    json.put("apiHost", apiHost);
    json.put("port", port);
    json.put("code", "200");
    return json.toString();
  }

}
